package com.wt.admin.service.language.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.aizuda.easy.security.code.BasicCode;
import com.wt.admin.code.language.Classification2000;
import com.wt.admin.domain.dto.language.ClassTrainingDTO;
import com.wt.admin.domain.dto.language.ClassificationDTO;
import com.wt.admin.domain.entity.language.ClassificationEntity;
import com.wt.admin.domain.entity.language.KeywordsEntity;
import com.wt.admin.domain.mp.language.ClassificationMP;
import com.wt.admin.domain.vo.language.ClassificationVO;
import com.wt.admin.mapper.language.ClassificationMapper;
import com.wt.admin.mapper.language.KeywordsMapper;
import com.wt.admin.service.language.ClassificationService;
import com.wt.admin.util.AssertUtil;
import com.wt.admin.util.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ClassificationServiceImpl implements ClassificationService {

    @Resource
    private ClassificationMapper classificationMapper;
    @Resource
    private KeywordsMapper keywordsMapper;

    @Transactional
    @Override
    public ClassificationVO add(ClassificationDTO data) {
        AssertUtil.Str.isEmpty(data.getClassName(), Classification2000.CODE_2000);
        ClassificationEntity classificationEntity = classificationMapper.findByClassName(data.getClassName());
        AssertUtil.objIsNotNull(classificationEntity, Classification2000.CODE_2003);
        classificationMapper.insert(data);
        if(CollUtil.isNotEmpty(data.getKeywords())){
            data.getKeywords().forEach(i -> i.setClassificationId(data.getId()));
            keywordsMapper.insertBatch(data.getKeywords());
        }
        return null;
    }

    @Override
    public List<ClassificationVO> list(ClassificationDTO data) {
        List<ClassificationVO> classificationVOS = new ArrayList<>();
        List<ClassificationMP> list = classificationMapper.list(data);
        Map<Integer, List<ClassificationMP>> collect = list.stream().collect(Collectors.groupingBy(ClassificationMP::getId));
        collect.keySet().stream().forEach(i -> {
            List<ClassificationMP> classificationMPs = collect.get(i);
            List<KeywordsEntity> keywords = classificationMPs.stream().map(j -> {
                if(ObjectUtil.isNull(j.getKeyId())){
                    return null;
                }
                KeywordsEntity keywordsEntity = new KeywordsEntity();
                keywordsEntity.setClassificationId(j.getId());
                keywordsEntity.setId(j.getKeyId());
                keywordsEntity.setKeyword(j.getKeyword());
                keywordsEntity.setReplies(j.getReplies());
                return keywordsEntity;
            }).filter(ObjectUtil::isNotNull).toList();
            ClassificationVO build = ClassificationVO.builder().keywords(keywords)
                    .sentenceCount(classificationMPs.get(0).getSentenceCount()).build();
            build.setId(classificationMPs.get(0).getId());
            build.setClassName(classificationMPs.get(0).getClassName());
            build.setCreateTime(classificationMPs.get(0).getCreateTime());
            build.setUpdateTime(classificationMPs.get(0).getCreateTime());
            classificationVOS.add(build);
        });
        return classificationVOS;
    }

    @Transactional
    @Override
    public ClassificationVO del(ClassificationDTO data) {
        AssertUtil.objIsNull(data.getId(), Classification2000.CODE_2001);
        classificationMapper.deleteById(data.getId());
        keywordsMapper.deleteByClassId(data.getId());
        return null;
    }

    @Transactional
    @Override
    public ClassificationVO edit(ClassificationDTO data) {
        AssertUtil.Str.isEmpty(data.getClassName(), Classification2000.CODE_2000);
        AssertUtil.objIsNull(data.getId(), Classification2000.CODE_2001);
        ClassificationEntity classificationEntity = classificationMapper.selectById(data.getId());
        AssertUtil.objIsNull(classificationEntity, BasicCode.BASIC_CODE_99998);
        classificationEntity.setClassName(data.getClassName());
        classificationMapper.updateById(classificationEntity);
        keywordsMapper.deleteByClassIdAndNotInKeyId(classificationEntity.getId(), data.getKeywords());
        if(CollUtil.isNotEmpty(data.getKeywords())){
            data.getKeywords().forEach(i -> i.setClassificationId(classificationEntity.getId()));
            keywordsMapper.insertOrUpdateBatch(data.getKeywords());
        }
        // 查询
        List<ClassificationVO> list = list(data);
        AssertUtil.objIsNull(list, Classification2000.CODE_2004);
        return list.get(0);
    }

    @Override
    public ClassificationEntity findById(Integer id) {
        return classificationMapper.selectById(id);
    }


}
